package com.dlc.shop.live.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.bo.ProductBO;
import com.dlc.shop.bean.enums.ProdStatusEnums;
import com.dlc.shop.bean.event.EsSearchProdEvent;
import com.dlc.shop.bean.param.EsProductParam;
import com.dlc.shop.bean.param.ProductParam;
import com.dlc.shop.bean.vo.search.EsProductSearchVO;
import com.dlc.shop.bean.vo.search.ProductSearchVO;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.dao.ProductMapper;
import com.dlc.shop.live.common.constant.LiveCacheNames;
import com.dlc.shop.live.common.dao.LiveRoomProdMapper;
import com.dlc.shop.live.common.model.LiveRoom;
import com.dlc.shop.live.common.model.LiveRoomProd;
import com.dlc.shop.live.common.service.LiveRoomProdService;
import com.dlc.shop.live.common.vo.LiveMsgItem;
import com.dlc.shop.live.common.vo.LiveRoomProdVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author TRACK
 */
@Service
@AllArgsConstructor
@Slf4j
public class LiveRoomProdServiceImpl extends ServiceImpl<LiveRoomProdMapper, LiveRoomProd> implements LiveRoomProdService {

    private final LiveRoomProdMapper liveRoomProdMapper;
    private final ProductMapper productMapper;
    private final ApplicationEventPublisher eventPublisher;

    @Override
    public void save(LiveRoom liveRoom) {
        if (CollUtil.isEmpty(liveRoom.getProdIds())) {
            return;
        }
        List<ProductBO> productBOList = getProductList(liveRoom.getProdIds());
        if (CollUtil.isEmpty(productBOList)) {
            return;
        }
        Long roomId = liveRoom.getRoomId();
        List<LiveRoomProd> saveList = new ArrayList<>(16);
        for (ProductBO productBO : productBOList) {
            LiveRoomProd liveRoomProd = new LiveRoomProd();
            liveRoomProd.setProdId(productBO.getProdId());
            liveRoomProd.setRoomId(roomId);
            liveRoomProd.setExplainStatus(0);
            saveList.add(liveRoomProd);
        }
        // 批量保存
        saveBatch(saveList);
    }

    private List<ProductBO> getProductList(List<Long> prodIds) {
        // 过滤掉状态不正常的商品
        ProductParam productParam = new ProductParam();
        productParam.setProdIds(prodIds);
        productParam.setStatus(ProdStatusEnums.NORMAL.getValue());
        return productMapper.listSimple(productParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = LiveCacheNames.LIVE_ROOM_PROD, key = "#liveRoom.roomId")
    public void update(LiveRoom liveRoom) {
        remove(new LambdaQueryWrapper<LiveRoomProd>().eq(LiveRoomProd::getRoomId, liveRoom.getRoomId()));
        save(liveRoom);
    }

    @Override
    @CacheEvict(cacheNames = LiveCacheNames.LIVE_ROOM_PROD, key = "#roomId")
    public void delete(Long roomId) {
        remove(new LambdaQueryWrapper<LiveRoomProd>().eq(LiveRoomProd::getRoomId, roomId));
    }

    @Override
    @Cacheable(cacheNames = LiveCacheNames.LIVE_ROOM_PROD, key = "#roomId")
    public List<LiveRoomProdVO> listLiveRoomProd(Long roomId) {
        return liveRoomProdMapper.listLiveRoomProd(roomId);
    }

    @Override
    @CacheEvict(cacheNames = LiveCacheNames.LIVE_ROOM_PROD, key = "#roomId")
    public int updateExplainStatus(Long roomId, Long prodId, Integer explainStatus) {
        return liveRoomProdMapper.updateExplainStatus(roomId, prodId, explainStatus);
    }

    @Override
    public List<LiveRoomProdVO> listLiveRoomProdInfo(Long roomId) {
        return liveRoomProdMapper.listLiveRoomProdInfo(roomId, I18nMessage.getLang());
    }

    @Override
    public LiveMsgItem handleLiveProd(LiveMsgItem liveMsgItem) {
        EsSearchProdEvent esSearchProdEvent = new EsSearchProdEvent();
        EsProductParam productParam = new EsProductParam();
        productParam.setProdId(liveMsgItem.getProdId());
        productParam.setStatus(ProdStatusEnums.NORMAL.getValue());
        PageParam<LiveRoomProdVO> page = new PageParam<>();
        page.setSize(1);
        page.setCurrent(1);
        esSearchProdEvent.setProductParam(productParam);
        esSearchProdEvent.setPageParam(page);
        eventPublisher.publishEvent(esSearchProdEvent);
        PageParam<EsProductSearchVO> productSearchPage = esSearchProdEvent.getProductSearchPage();
        EsProductSearchVO esProductSearchVO = productSearchPage.getRecords().get(0);
        if (Objects.isNull(esProductSearchVO) || CollUtil.isEmpty(esProductSearchVO.getProducts())) {
            return null;
        }
        ProductSearchVO productSearchVO = esProductSearchVO.getProducts().get(0);
        liveMsgItem.setPic(productSearchVO.getPic());
        liveMsgItem.setPrice(productSearchVO.getPrice());
        liveMsgItem.setProdName(productSearchVO.getProdName());
        liveMsgItem.setActivityPrice(productSearchVO.getActivityPrice());
        liveMsgItem.setProdLangList(productSearchVO.getProdLangList());
        return liveMsgItem;
    }
}
